package com.yunchang.springboot.config;

import com.yunchang.springboot.mapper.shop.ShopAreaMapper;
import com.yunchang.springboot.mapper.shop.ShopMapper;
import com.yunchang.springboot.models.Area;
import com.yunchang.springboot.models.Shop;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

//@Configuration
@Component
@Getter
public class MWSReportThrottleConfig implements ApplicationRunner {

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    ShopAreaMapper shopAreaMapper;

    public static ConcurrentHashMap<String, Integer> REQUEST_REPORT_THROTTLE_COUNT_MAP = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<String, Integer> GET_REPORT_REQUEST_LIST_THROTTLE_COUNT_MAP = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<String, Integer> GET_REPORT_LIST_THROTTLE_COUNT_MAP = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<String, Integer> GET_REPORT_THROTTLE_COUNT_MAP = new ConcurrentHashMap<>();

    public static List<Shop> SHOP_LIST = new ArrayList<>();

    public void setRequestReportThrottleCountMap() {
        List<Shop> shopList = shopMapper.getAllList();
        for (Shop shop : shopList) {
            String sellerId = shop.getSellerId();
            REQUEST_REPORT_THROTTLE_COUNT_MAP.put(sellerId, 15);
        }
    }

    public void setGetReportRequestListThrottleCountMap() {
        List<Shop> shopList = shopMapper.getAllList();
        for (Shop shop : shopList) {
            String sellerId = shop.getSellerId();

            GET_REPORT_REQUEST_LIST_THROTTLE_COUNT_MAP.put(sellerId, 10);

        }
    }

    public void setGetReportListThrottleCountMap() {
        List<Shop> shopList = shopMapper.getAllList();
        for (Shop shop : shopList) {
            String sellerId = shop.getSellerId();
            GET_REPORT_LIST_THROTTLE_COUNT_MAP.put(sellerId, 10);
        }
    }

    public void setGetReportThrottleCountMap() {
        List<Shop> shopList = shopMapper.getAllList();
        for (Shop shop : shopList) {
            String sellerId = shop.getSellerId();
            GET_REPORT_THROTTLE_COUNT_MAP.put(sellerId, 15);
        }
    }

    public void setShopList() {
        List<Shop> shopList = shopMapper.getAllList();
        for (Shop shop : shopList) {
//            List<Area> areaListByShopId = shopAreaMapper.getAreaListByShopId(shop.getId());
//            shop.setAreas(areaListByShopId);
            SHOP_LIST.add(shop);
        }
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        setShopList();
        setRequestReportThrottleCountMap();
        setGetReportRequestListThrottleCountMap();
        setGetReportListThrottleCountMap();
        setGetReportThrottleCountMap();
        System.out.println("程序启动完成,启动配额守护线程");
        //map维护完成后开始调用线程来维护map配合
        Thread daemon = new Thread(() -> {
            System.out.println("各接口配额维护开始");
            while (true) {
                try {
                    List<Shop> shopList = SHOP_LIST;
                    for (Shop shop : shopList) {
                        String sellerId = shop.getSellerId();
                        if (REQUEST_REPORT_THROTTLE_COUNT_MAP.get(sellerId) < 15) {
                            REQUEST_REPORT_THROTTLE_COUNT_MAP.replace(sellerId, REQUEST_REPORT_THROTTLE_COUNT_MAP.get(sellerId) + 1);
                            System.out.println(sellerId + "的REQUEST_REPORT_THROTTLE_COUNT:" + REQUEST_REPORT_THROTTLE_COUNT_MAP.get(sellerId));
                        }
                        if (GET_REPORT_REQUEST_LIST_THROTTLE_COUNT_MAP.get(sellerId) < 10) {
                            GET_REPORT_REQUEST_LIST_THROTTLE_COUNT_MAP.replace(sellerId, GET_REPORT_REQUEST_LIST_THROTTLE_COUNT_MAP.get(sellerId) + 1);
                            System.out.println(sellerId + "的GET_REPORT_REQUEST_LIST_THROTTLE_COUNT:" + GET_REPORT_REQUEST_LIST_THROTTLE_COUNT_MAP.get(sellerId));
                        }
                        if (GET_REPORT_LIST_THROTTLE_COUNT_MAP.get(sellerId) < 10) {
                            GET_REPORT_LIST_THROTTLE_COUNT_MAP.replace(sellerId, GET_REPORT_LIST_THROTTLE_COUNT_MAP.get(sellerId) + 1);
                            System.out.println(sellerId + "的GET_REPORT_LIST_THROTTLE_COUNT:" + GET_REPORT_LIST_THROTTLE_COUNT_MAP.get(sellerId));
                        }
                        if (GET_REPORT_THROTTLE_COUNT_MAP.get(sellerId) < 15) {
                            GET_REPORT_THROTTLE_COUNT_MAP.replace(sellerId, GET_REPORT_THROTTLE_COUNT_MAP.get(sellerId) + 1);
                            System.out.println(sellerId + "的GET_REPORT_THROTTLE_COUNT:" + GET_REPORT_THROTTLE_COUNT_MAP.get(sellerId));
                        }
                    }
                    Thread.sleep(60000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("+1");
                }
            }
        });
        daemon.setDaemon(true);
        daemon.start();
    }
}
